package com.demo.security.custom;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import com.demo.security.entity.SysUser;
import com.demo.security.model.SysUserDetails;
import com.demo.security.utils.jackson.JacksonUtils;

@Service
public class SysUserDetailsService implements UserDetailsService {

    private static final Logger log = LoggerFactory.getLogger(SysUserDetailsService.class);

    @Autowired
    private PasswordEncoder passwordEncoder;

    Map<String, SysUserDetails> userMap = new HashMap<String, SysUserDetails>();

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        log.info(">>>>>>>>>>>>>>>> loadUserByUsername: {}", username);
        // 从数据库获取登录账号信息
        SysUserDetails user = userMap.get(username);
        if (user != null) {
            this.setAuthorities(user);
            log.info(">>>>>>>>>>>>>>>> UserDetails: {}", JacksonUtils.toJsonString(user));
        }
        return user;
    }

    private void setAuthorities(SysUserDetails user) {
        List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
        if (user.getUsername().equals("admin") || user.getUsername().equals("user")) {
            authorities.add(new GrantedAuthority() {
                private static final long serialVersionUID = 1L;

                @Override
                public String getAuthority() {
                    return "ROLE_USER";
                }
            });
        }
        if (user.getUsername().equals("user")) {
            authorities.add(new GrantedAuthority() {
                private static final long serialVersionUID = 1L;

                @Override
                public String getAuthority() {
                    return "ROLE_user";
                }
            });
        }
        if (user.getUsername().equals("admin")) {
            authorities.add(new GrantedAuthority() {
                private static final long serialVersionUID = 1L;

                @Override
                public String getAuthority() {
                    return "admin";
                }
            });
        }
        user.setAuthorities(authorities);
    }

    @PostConstruct
    public void init() {
        userMap.put("admin", new SysUserDetails(new SysUser(1L, "admin", passwordEncoder.encode("123456"), true)));
        userMap.put("user", new SysUserDetails(new SysUser(1L, "user", passwordEncoder.encode("123456"), true)));
        userMap.put("zs", new SysUserDetails(new SysUser(1L, "zs", passwordEncoder.encode("123456"), true)));
        userMap.put("ls", new SysUserDetails(new SysUser(1L, "ls", passwordEncoder.encode("123456"), false)));
        userMap.put("ww", new SysUserDetails(new SysUser(1L, "ww", "123456", true)));
    }
}
